package de.hwrberlin.remo.diagram.custom.decorators;

import java.util.List;
import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.AbstractDecorator;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Image;
import de.hwrberlin.remo.diagram.custom.util.ImageUtil;
import de.hwrberlin.remo.diagram.custom.util.Images;
import de.hwrberlin.remo.model.ElementState;
import de.hwrberlin.remo.model.LinkObject;
import de.hwrberlin.remo.model.RemoModelPackage;


public class LinkStateDecorator extends AbstractDecorator implements
		NotificationListener {

	public LinkStateDecorator(IDecoratorTarget decoratorTarget) {
		super(decoratorTarget);	
		removeListeners();
		addListeners();
	}

	@Override
	public void activate() {
		refresh();
	}

	public void refresh() {
		Image image = null;
		removeDecoration();
		ConnectionEditPart ceditPart = (ConnectionEditPart) getDecoratorTarget().getAdapter(ConnectionEditPart.class);
		IFigure figure = ceditPart.getFigure();
		List<?> figurelist = figure.getChildren();
		Object wrappinglabel = figurelist.get(0);
		WrappingLabel label = (WrappingLabel) wrappinglabel;
		image = ImageUtil.getImage(Images.NOT_IMAGE);
		
		View view = (View) getDecoratorTarget().getAdapter(View.class);
		LinkObject link = (LinkObject) view.getElement();
			if (link.getLinkstate() == ElementState.FALSE) {
				label.setIcon(null);
			}
			if (link.getLinkstate() == ElementState.TRUE) {
				label.setIcon(image);
			}
	}
	
	@Override
	public void notifyChanged(Notification notification) {
		Object feature = notification.getFeature();

		if(RemoModelPackage.Literals.LINK_OBJECT__LINKSTATE.equals(feature)) {
			refresh();
		} 
	}

	private void addListeners() {
		ConnectionEditPart editPart = (ConnectionEditPart) getDecoratorTarget().getAdapter(ConnectionEditPart.class);

		if (editPart != null) {
			LinkObject lElement = (LinkObject) editPart.resolveSemanticElement();
			if (lElement == null || lElement.eIsProxy()) {
				return;
			}
			TransactionalEditingDomain theEditingDomain = editPart.getEditingDomain();
			
			if (theEditingDomain != null) {
				DiagramEventBroker diagramEventBroker = DiagramEventBroker.getInstance(theEditingDomain);
				
				diagramEventBroker.addNotificationListener(lElement, this);
			}
		}
	}
	
	private void removeListeners() {
		ConnectionEditPart editPart = (ConnectionEditPart) getDecoratorTarget()
				.getAdapter(ConnectionEditPart.class);
		
		if (editPart != null) {
			LinkObject lElement = (LinkObject) editPart.resolveSemanticElement();
			if (lElement == null || lElement.eIsProxy()) {
				return;
			}
			TransactionalEditingDomain theEditingDomain = editPart.getEditingDomain();
			
			if (theEditingDomain != null) {
				DiagramEventBroker diagramEventBroker = DiagramEventBroker.getInstance(theEditingDomain);
				
				diagramEventBroker.removeNotificationListener(lElement, this);
			}
		}
	}
}
